package cn.aop;


import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.Arrays;


public class ErrorLogger {
    private static final Logger log=Logger.getLogger(ErrorLogger.class);
    public void afterThrowing(JoinPoint jp,RuntimeException e){
        log.error(jp.getSignature().getName()+"方法发生异常："+e);
    }


    public void afterLogger(JoinPoint jp) throws Throwable {
        log.error(jp.getSignature().getName()+"执行结束");
    }


    public Object aroundLogger(ProceedingJoinPoint jp) throws Throwable {
        log.info("调用 "+jp.proceed()+" 的 "+jp.getSignature().getName()+" 方法。方法入参："+ Arrays.toString(jp.getArgs()));
        try {
            Object result = jp.proceed();//执行目标方法并获得返回值
            log.info("调用 "+jp.getSignature()+" 的 "+jp.getSignature().getName()+" 方法。方法返回值："+result);
            return result;
        }catch (Throwable e){
            log.error(jp.getSignature().getName()+" 方法发生异常："+e);
            throw e;
        }finally {
            log.info(jp.getSignature().getName()+" 方法执行结束");
        }
    }
}
